{extend name="public:base" /}
{block name="js"}
<script type="text/javascript" src="__STATIC__/js/plugins/chosen/chosen.jquery.js"></script>

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-content">
                    <form method="post" id="user-form" class="ajax-form" action="{:url('add')}">
                        <input name="uid" type="hidden">
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">手机号码</label>
                                <div class="col-sm-3" autocomplete="off">
                                    <input type="text" name="account" id="accountCheckIdentity"
                                           autocomplete="off"
                                           class="form-control required-validate" placeholder="请输入手机号码">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">密码</label>
                                <div class="col-sm-3">
                                    <input type="password" placeholder="请输入密码，不填则默认密码：1234678" class="form-control" name="password">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">姓名</label>
                                <div class="col-sm-3"><input type="text" name="username" class="form-control"
                                                             placeholder="请输入姓名"></div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">状态<br/>
                                <small class="text-navy"></small></label>
                                <div class="col-sm-3">
                                    <div class="i-checks">
                                        <label> <input type="radio" value="1" name="status" checked><i></i> 开启 </label>
                                    </div>
                                    <div class="i-checks">
                                        <label> <input type="radio" value="2" name="status"><i></i> 锁定 </label>
                                    </div>
									<div class="i-checks">
                                        <label> <input type="radio" value="3" name="status"><i></i> 禁用 </label>
                                    </div>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">角色</label>

                                <div class="col-sm-3">
                                    <select class="form-control m-b" name="role">
                                        {volist name="roles" id="entity"}
                                        <option value="{$entity.id}">{$entity.name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">部门</label>
                                <div class="col-sm-3">
                                    {volist name="departments" id="entity" key="key"}
                                    <div class="i-checks">
                                        <label>
                                            <input type="checkbox" value="{$entity.id}" name="department_ids[]"> <i></i>
                                            {$entity.name}
                                        </label>
                                    </div>
                                    {/volist}
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="box-item">
                            <div class="form-group row"><label class="col-sm-1 col-form-label">职位</label>
                                <div class="col-sm-3">
                                    {volist name="positions" id="entity"}
                                    <div class="i-checks">
                                        <label>
                                            <input type="radio" value="{$entity.id}" name="position_id"><i></i>
                                            {$entity.name}
                                        </label>
                                    </div>
                                    {/volist}
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>
                        <div class="form-group row">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button type="button" class="btn btn-w-m btn-default back-page">返回</button>
                                <button type="submit" class="btn btn-w-m btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

{/block}

{block name='js'}
<script type="text/javascript" src="__PLUG__/kindeditor/kindeditor-all.js"></script>
<script type="text/javascript" src="__ADMIN__/js/plugins/typehead/bootstrap3-typeahead.min.js"></script>
<script>
    $(document).ready(function () {
        <!-- 实例化编辑器 -->

        function split(val) {
            return val.split(/,\s*/);
        }

        function extractLast(term) {
            return split(term).pop();
        }

        $('#accountCheckIdentity').typeahead({
            source: function (query, process) {
                $.getJSON("{:url('search')}", {
                    account: extractLast(query)
                }, function (res) {
                    $form = $("#user-form");
                    $form.find("input[name='uid']").val("");
                    if (res.data.data.length <= 0) {
                        // if ($form.find("input[name='username']").attr("disabled")) {
                        //     $form.find("input[name='username']").removeAttr("disabled");
                        //     $form.find("input[name='username']").val("");
                        // }
                        // if ($form.find("input[name='mobile']").attr("disabled")) {
                        //     $form.find("input[name='mobile']").removeAttr("disabled");
                        //     $form.find("input[name='mobile']").val("");
                        // }
                    }
                    console.log(res.data.data);
                    process(res.data.data)
                });
            },
            updater: function (item) {
                console.log("选中一个,账号为：", item.account)
                return item;
            },
            displayText: function (item) {
                return item.account;
            },
            afterSelect: function (item) {
                console.log(111)
                let userForm = $("#user-form");
                userForm.find("input[name='account']").val(item.account);
                userForm.find("input[name='uid']").val(item.id);
                userForm.find("input[name='username']").val(item.username);
                // userForm.find("input[name='username']").attr("disabled", true);
                // userForm.find("input[name='mobile']").val(item.mobile);
                // userForm.find("input[name='mobile']").attr("disabled", true);
                return item;
            }
        });

        function updateImg() {
            // $("#upload_img").click(function () {
            $("#img_file").trigger("click");
            // })

            $('#img_file').change(function () {
                var pic = $(this)[0].files[0];
                var formData = new FormData();
                var url = "{:url('UploadFile/uploadPicture',array('session_id'=>session_id()))}";
                formData.append('download', pic);
                $.ajax({
                    url: url,
                    type: "post",
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        if (data.status === 1) {
                            //上传成功
                            var id = data.id;
                            var path = data.path;
                            console.log(data);
                            return;

                            $('#img').val(id);
                            $('#uploadImg').attr('src', path);
                        } else {
                            layer.msg(data.info);
                        }
                    }
                });
            });
        }
    })
</script>
{/block}
